package edu.gatech.cs2340.teamrocket.view.networking;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import edu.gatech.cs2340.teamrocket.control.guicontrol.networking.NetworkSetupControlInterface;
import edu.gatech.cs2340.teamrocket.control.networking.impl.VTNetworkingInterface;

/**
 * GUI frame that asks users for info needed to setup or join a server.
 *
 * @author Joe Tacheron
 */
public class NetworkSetupGUI implements NetworkSetupGUIInterface {

    private final NetworkSetupControlInterface control;

    private JFrame frame;
    private JLabel errClient;
    private JLabel errServer;
    private JTextField hostField;
    private JTextField clientUserField;
    private JTextField clientPassField;
    private JTextField serverUserField;
    private JTextField serverPassField;
    private JButton startClient;
    private JButton startServer;
    private JButton clientCancel;
    private JButton serverCancel;

    public NetworkSetupGUI(NetworkSetupControlInterface c) {
        this.control = c;
        this.control.setGUI(this);

        //error messages label
        errClient = new JLabel();
        errServer = new JLabel();

        //user inputs
        hostField = new JTextField("127.0.0.1");
        clientUserField = new JTextField();
        clientPassField = new JPasswordField();
        serverUserField = new JTextField();
        serverPassField = new JPasswordField();

        //buttons
        startClient = new JButton("Join Server");
        startServer = new JButton("Create Server");
        clientCancel = new JButton("Cancel");
        serverCancel = new JButton("Cancel");

        startClient.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
                control.startClientClicked();
            }
        });
        startServer.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
                control.startServerClicked();
            }
        });
        clientCancel.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
                control.cancelClicked();
            }
        });
        serverCancel.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
                control.cancelClicked();
            }
        });
    }

    @Override
    public void createAndShowGUI() {
        frame = new JFrame("Network Gaming");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(500, 250);
        frame.setResizable(false);

        JPanel clientPanel = new JPanel();
        JPanel serverPanel = new JPanel();
        clientPanel.setLayout(new GridLayout(5,2));

        //join server part
        clientPanel.setBorder(BorderFactory.createTitledBorder("Join Server"));
        clientPanel.add(new JLabel("IP Address:"));
        clientPanel.add(hostField);
        clientPanel.add(new JLabel("Username:"));
        clientPanel.add(clientUserField);
        clientPanel.add(new JLabel("Password:"));
        clientPanel.add(clientPassField);
        clientPanel.add(startClient);
        clientPanel.add(clientCancel);
        clientPanel.add(errClient);

        //create server part
        serverPanel.setLayout(new GridLayout(5,2));
        serverPanel.setBorder(BorderFactory.createTitledBorder("Create Server"));
        serverPanel.add(new JLabel("Your IP Address: "));
        serverPanel.add(new JLabel(VTNetworkingInterface.getInstance().getIPAddress()));
        serverPanel.add(new JLabel("Username:"));
        serverPanel.add(serverUserField);
        serverPanel.add(new JLabel("Password:"));
        serverPanel.add(serverPassField);
        serverPanel.add(startServer);
        serverPanel.add(serverCancel);
        serverPanel.add(errServer);

        JTabbedPane tab  = new JTabbedPane();
        tab.add("Create", serverPanel);
        tab.add("Join", clientPanel);

        frame.setLayout(new GridLayout(1, 1));
        frame.add(tab);
        //frame.add(centerPanel);
        //frame.add(cancel);

        frame.setVisible(true);
    }

    @Override
    public String getIPAddress() {
        return hostField.getText();
    }

    @Override
    public String getClientUsername() {
        return clientUserField.getText();
    }

    @Override
    public String getClientPassword() {
        return clientPassField.getText();
    }

    @Override
    public String getServerUsername() {
        return serverUserField.getText();
    }

    @Override
    public String getServerPassword() {
        return serverPassField.getText();
    }

    @Override
    public void errIPAddress(String errMsg) {
    	errClient.setText("<html><p color='red'>" + errMsg + "</p></html>");
    }

    @Override
    public void errClientUsername(String errMsg) {
        errClient.setText("<html><p color='red'>" + errMsg + "</p></html>");
    }

    @Override
    public void errServerUsername(String errMsg) {
        errServer.setText("<html><p color='red'>" + errMsg + "</p></html>");
    }

    @Override
    public void errClientPassword(String errMsg) {
    	errClient.setText("<html><p color='red'>" + errMsg + "</p></html>");
    }

    @Override
    public void errServerPassword(String errMsg) {
    	errServer.setText("<html><p color='red'>" + errMsg + "</p></html>");
    }

    @Override
    public void close() {
        frame.setVisible(false);
        frame.dispose();
    }

}
